package online.yysh.charging;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import online.yysh.charging.base.BaseActivity;
import online.yysh.charging.base.MyViewPager;
import online.yysh.charging.fragment.MapViewFragment;
import online.yysh.charging.fragment.MineFragment;
import online.yysh.charging.fragment.OrderFragment;

import java.util.ArrayList;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    private MyViewPager vp;
    private ArrayList<Fragment> fragments;
    private MapViewFragment mapViewFragment;
    private OrderFragment orderFragment;
    private MineFragment mineFragment;
    private MyViewPagerAdapter adapter;

    private boolean isFirstMapViewFragment;

    private RadioGroup rg;
    private RadioButton rb_charging, rb_order, rb_mine;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        vp = findViewById(R.id.vp);
        vp.setCanScroll(false);
        rg = findViewById(R.id.rg);
        rb_charging = findViewById(R.id.rb_charging);
        rb_mine = findViewById(R.id.rb_mine);
//        rb_order = findViewById(R.id.rb_order);

        rg.setOnCheckedChangeListener(this);
        rg.check(R.id.rb_charging);
    }

    @Override
    protected void initData() {
        mapViewFragment = new MapViewFragment();
        orderFragment = new OrderFragment();
        mineFragment = new MineFragment();
        fragments = new ArrayList<>();
        fragments.add(mapViewFragment);
//        fragments.add(orderFragment);
        fragments.add(mineFragment);
        adapter = new MyViewPagerAdapter(getSupportFragmentManager());
        vp.setAdapter(adapter);
        vp.setCurrentItem(0);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int id) {
        setItem(id);
    }

    class MyViewPagerAdapter extends FragmentPagerAdapter {
        public MyViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            if (position == 0) {
                return;
            }
            super.destroyItem(container, position, object);
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            if (position == 0) {
                if (!isFirstMapViewFragment) {
                    isFirstMapViewFragment = true;
                }
            }
            return super.instantiateItem(container, position);
        }
    }

    /**
     * 切换vp显示页面
     * @param id
     */
    public void setItem(int id) {
        int currentItem = 0;
        Drawable homeFalse = getResources().getDrawable(R.mipmap.home_false);
        setBounds(homeFalse);
        Drawable homeTrue = getResources().getDrawable(R.mipmap.home_true);
        setBounds(homeTrue);
        Drawable mineFalse = getResources().getDrawable(R.mipmap.mine_false);
        setBounds(mineFalse);
        Drawable mineTrue = getResources().getDrawable(R.mipmap.mine_true);
        setBounds(mineTrue);
        switch (id) {
            case R.id.rb_charging:
                currentItem = 0;
//                rb_charging.setBackgroundResource(R.color.gray);
//                rb_order.setBackgroundResource(R.color.white);
//                rb_mine.setBackgroundResource(R.color.white);

//                rb_charging.setTextColor(getResources().getColor(R.color.blue2));
//                rb_mine.setTextColor(getResources().getColor(R.color.gray));

                rb_charging.setCompoundDrawables(null,homeTrue,null,null);
                rb_mine.setCompoundDrawables(null,mineFalse,null,null);
//                rb_charging.setBackgroundResource(R.color.white);
//                rb_mine.setBackgroundResource(R.color.white5);
                break;
//            case R.id.rb_order:
//                currentItem = 1;
//                rb_charging.setBackgroundResource(R.color.white);
//                rb_order.setBackgroundResource(R.color.gray);
//                rb_mine.setBackgroundResource(R.color.white);
//                break;
            case R.id.rb_mine:
                currentItem = 2;
//                rb_charging.setBackgroundResource(R.color.white);
//                rb_order.setBackgroundResource(R.color.white);
//                rb_mine.setBackgroundResource(R.color.gray);

//                rb_mine.setTextColor(getResources().getColor(R.color.blue2));
//                rb_charging.setTextColor(getResources().getColor(R.color.gray));

                rb_charging.setCompoundDrawables(null,homeFalse,null,null);
                rb_mine.setCompoundDrawables(null,mineTrue,null,null);
//                rb_charging.setBackgroundResource(R.color.white5);
//                rb_mine.setBackgroundResource(R.color.white);
                break;
        }
        vp.setCurrentItem(currentItem);
    }

    public void setBounds(Drawable drawable) {
        float scale = getResources().getDisplayMetrics().density;
        drawable.setBounds(0, 0, (int) (30 * scale + 0.5f), (int) (30 * scale + 0.5f));
    }
}